<template>
  <view class="viewport">
    <!-- #ifndef MP-TOUTIAO -->
    <!-- 自定义导航栏 -->
    <uni-nav-bar class="uni-nav-bar" background-color="rgba(255,255,255,0)" :border="false" :statusBar="true"
      @clickLeft="goBack">
      <template #left>
        <view class="icon-arrow-left"></view>
      </template>
      <template #default>
        <view class="title">章节练习</view>
      </template>
    </uni-nav-bar>
    <!-- #endif -->
    <!-- 抖音小程序顶部导航 -->
    <!-- #ifdef MP-TOUTIAO -->
    <view class="toutiao-nav-bar">
      <view class="status-bar"></view>
      <view class="title-container">
        <view class="title">章节练习</view>
      </view>
    </view>
    <!-- #endif -->
    <scroll-view class="scroll-view" enable-back-to-top scroll-y>
      <view class="chapter-exercise">
        <view class="chapter-exercise-item" v-for="(item, index) in chapterList" :key="index"
          @tap="toExercise(item.code!)">
          <view class="item-name">{{ index + 1 }}. {{ item.name }}</view>
          <view class="item-num">{{ useGetChapterExercisesQuestions(baseType!, item.code!).length }}题</view>
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<script lang="ts" setup>
  import { onLoad } from '@dcloudio/uni-app'
  import { ref } from 'vue'
  import type { LearnBaseClassInfo } from '@/api/base/types'

  import { useGetChapterExercises, useGetChapterExercisesQuestions } from '@/composables/Exercise'

  import { ExerciseTypeEnum } from '@/api/constants'
  // #ifdef MP-TOUTIAO
  // 获取小程序下胶囊按钮的布局位置信息
  let menuButtonInfo = uni.getMenuButtonBoundingClientRect()
  // 上边界坐标，单位：px
  let top = (menuButtonInfo.top - 10) + 'px'
  const statusBarHeight = ref(top)
  // 高度，单位：px
  let height = (menuButtonInfo.height + 20) + 'px'
  const titleHeight = ref(height)
  // #endif
  const baseType = ref<string>()
  const chapterList = ref<LearnBaseClassInfo[]>([])
  onLoad((options: any) => {
    baseType.value = options.baseType
    chapterList.value = useGetChapterExercises(baseType.value!) || []
  })

  // 去练习
  const toExercise = (code: string) => {
    uni.navigateTo({
      url: `/pagesExercise/answer/answer?exerciseType=${ExerciseTypeEnum.ChapterExerciseQuestions}&baseType=${baseType.value}&code=${code}`
    })
  }

  //退出
  const goBack = () => {
    uni.navigateBack()
  }
</script>

<style lang="scss" scoped>
  .viewport {
    display: flex;
    flex-direction: column;
  }

  .uni-nav-bar {
    .icon-arrow-left {
      font-weight: bold;
    }

    .title {
      align-self: center;
      flex: 1;
      text-align: center;
      font-size: 32rpx;
      font-weight: bold;
    }
  }

  /* #ifdef MP-TOUTIAO */
  // 抖音自定义导航栏
  .toutiao-nav-bar {

    // 顶部占位
    .status-bar {
      width: 100vw;
      height: v-bind(statusBarHeight);
    }

    // 标题行
    .title-container {
      display: flex;
      flex-direction: row;
      align-items: center;
      // justify-content: center;
      height: v-bind(titleHeight);

      .title {
        margin-left: 82rpx;
        font-size: 32rpx;
        // font-weight: bold;
      }
    }
  }

  /* #endif */
  .scroll-view {
    display: flex;
    flex-direction: column;
    padding: 0rpx 30rpx;
  }

  // 章节练习
  .chapter-exercise {
    display: flex;
    flex-direction: column;
    font-size: 28rpx;
    color: #323232;

    .chapter-exercise-item {
      display: flex;
      justify-content: space-between;
      padding: 36rpx 0;

      .item-num {
        color: #858585;
      }
    }
  }
</style>
